﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Capnhathocsinh : System.Web.UI.Page
{
    BUS_ChildrenClass objC = new BUS_ChildrenClass();
    BUS_TeacherClass objT = new BUS_TeacherClass();
    DTO_ChildrenClass dtoC = new DTO_ChildrenClass();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["TeacherLogin"] != null)
            {
                Hienthi();
            }
            else Response.Redirect("dangnhap.aspx?gv=1");
        }
            
    }
    public void Hienthi()
    {
        string teacherid = Session["username"].ToString();
        var dataT = objT.TeacherClass_getAllByTeacherid(teacherid);
        if (dataT.Rows.Count > 0)
        {
            string classid = dataT.Rows[0]["classid"].ToString();
            gvdshs.DataSource = objC.ChildrenClass_getAllByClassid(classid);
            gvdshs.DataBind();
        }
    }

    protected void gvdshs_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvdshs.EditIndex = e.NewEditIndex;
        Hienthi();
    }
    protected void gvdshs_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvdshs.EditIndex = -1;
        Hienthi();
    }
    protected void gvdshs_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvdshs.PageIndex = e.NewPageIndex;
        Hienthi();
    }
    protected void gvdshs_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("style", "background-color: #fff ; color: #000;");
                e.Row.Attributes.Add("onmouseover", "style.backgroundColor='#ccc'");
                e.Row.Attributes.Add("onmouseout", "style.backgroundColor='#fff'");
            }
        }
        catch
        { }
    }
    protected void gvdshs_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int index = gvdshs.EditIndex;
        dtoC.ID = int.Parse(gvdshs.Rows[index].Cells[0].Text);
        dtoC.Height = (gvdshs.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text;
        dtoC.Weight = (gvdshs.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text;
        dtoC.Healthy = (gvdshs.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox).Text;
        if (objC.ChildrenClass_Update(dtoC))
        {
            gvdshs.EditIndex = -1;
            Hienthi();
        }
        else Response.Write("<script>alert(\"Cập nhật không thành công.\")</script>");
    }
}